package com.cyd.product_service.controller;


import com.cyd.product_service.domain.Product;
import com.cyd.product_service.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/product")
//@RefreshScope   //动态刷新的
public class ProductController  {

    @Value("${server.port}")
    private String port;

    @Value("${env}")
    private String env;

    @Autowired
    private ProductService productService;

    /**
     * 商品列表
     * @return
     */
    @RequestMapping("list")
    public Object list(){

        return productService.productList();
    }

    /**
     * 商品ID查询
     * @return
     */
    @RequestMapping("findById")
    public Object findById(@RequestParam("id") int id){
//        try {
//            TimeUnit.SECONDS.sleep(1);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }


        Product product=productService.findById(id);
        Product result=new Product();
        BeanUtils.copyProperties(product,result);
//        result.setName(product.getName()+"测试端口="+port );
        result.setName( result.getName() + " data from port="+port +" env = "+env );
        return result;
    }
}
